//
//  ChatViewController.m
//  map
//
//  Created by dllo on 16/4/23.
//  Copyright © 2016年 王彦森. All rights reserved.
//

#import "ChatViewController.h"
#import "EaseMob.h"
#import "AddViewController.h"
#import "FriendViewController.h"
@interface ChatViewController ()<EMChatManagerDelegate>
@property(nonatomic,strong)UITextField *name;//账户
@property(nonatomic,strong)UITextField *passWord;//密码
@property(nonatomic,strong)UIButton *registerBut;//注册
@property(nonatomic,strong)UIButton *loginBut;//登录
@end

@implementation ChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.name = [[UITextField alloc]initWithFrame:CGRectMake(50, 90, 150, 50)];
    self.name.placeholder = @"请输入账户";
    self.name.textAlignment = YES;
    [self.view addSubview:self.name];
    
    self.passWord = [[UITextField alloc]initWithFrame:CGRectMake(50, 150, 150, 50)];
    self.passWord.placeholder = @"请输入密码";
    self.passWord.textAlignment = YES;
    [self.view addSubview:self.passWord];
    
    
    self.registerBut = [UIButton buttonWithType:UIButtonTypeSystem];
    self.registerBut.backgroundColor =[UIColor redColor];
    self.registerBut.frame =CGRectMake(50, 210, 60, 40);
    [self.registerBut setTitle:@"注册'" forState:UIControlStateNormal];
    [self.registerBut addTarget:self action:@selector(registerBut:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.registerBut];
    
    self.loginBut = [UIButton buttonWithType:UIButtonTypeSystem];
    self.loginBut.backgroundColor = [UIColor redColor];
    self.loginBut.frame =CGRectMake(150,210 , 60, 40);
    [self.loginBut setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginBut addTarget:self action:@selector(loginBut:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginBut];
    

    
}

/*
 LastLoginTime = 1461404898268;
 jid = "c9876543210#csy123456_a@easemob.com";
 password = 1;
 resource = mobile;
 
 //令牌
 token = "YWMtgZQtUgk4EeaOSmMLQwiwEQAAAVV3gqIYJcrNuAHQ_NGJJUZG8pJJLxXNim0";
 username = a;

 
 */


#pragma mark 登录的点击方法
-(void)loginBut:(UIButton*)but{
    
    NSString *userName = self.name.text;
    NSString *passWord = self.passWord.text;
    
    if (userName.length == 0||passWord.length == 0) {
        NSLog(@"请输入账户和密码");
        return;
    }
    //登录
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:userName password:passWord completion:^(NSDictionary *loginInfo, EMError *error) {
        if (!error) {
            //            loginInfo:登录信息
            NSLog(@"登录成功 %@",loginInfo);
           FriendViewController  *add = [[FriendViewController alloc]init];
            [self presentViewController:add animated:YES completion:^{
//
            }];


            
        }else{
            NSLog(@"登录失败 %@",error);
        }
        
        //全局队列
    } onQueue:dispatch_get_main_queue()];


}

-(void)registerBut:(UIButton*)but{
    NSString *userName = self.name.text;
    NSString *passWord = self.passWord.text;
    
    if (userName.length == 0||passWord.length == 0) {
        NSLog(@"请输入账户和密码");
        return;
    }
    
    //注册
    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:userName password:passWord withCompletion:^(NSString *username, NSString *password, EMError *error) {
        //block 是在哪个线程调用的
        NSLog(@"%@",[NSThread currentThread]);
        if (!error) {
            NSLog(@"注册成功");
        }else{
            NSLog(@"%@",error);
        }
       
    } onQueue:dispatch_get_main_queue()];
    
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.name resignFirstResponder];
    [self.passWord resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
